import { useHttp } from "./http";
import { QueryKey, useMutation, useQuery } from "react-query";
import {
  useAddConfig,
  useDeleteConfig,
  useEditConfig,
} from "./useOptimisticOptions";
import { Project } from "../types/project";
import { clearObject } from "./index";

export const useProjects = (param?: Partial<Project>) => {
  const client = useHttp();

  //param 变化的时候会触发
  return useQuery<Project[]>(["projects", clearObject(param)], () =>
    client("projects", { data: param })
  );

  // return useQuery<Project[], Error>(['projects', param], () => client("projects", {data: param}));

  // const client = useHttp();
  // const { run, ...result } = useAsync<Project[]>();
  // const fetchProjects = useCallback(
  //   () => client("projects", { data: clearObject(param || {}) }),
  //   [client, param]
  // );
  // useEffect(() => {
  //   // setIsLoading(true);
  //   run(fetchProjects(), { retry: fetchProjects }).then();
  //   // .then(setList)
  //   // .catch((err) => {
  //   // setList([]);
  //   // setError(err);
  //   // })
  //   // .finally(() => setIsLoading(false));
  // }, [param, run, fetchProjects]);
  // return result;
};

/*
 * 编辑
 * */
export const useEditProject = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (params: Partial<Project>) =>
      client(`projects/${params.id}`, {
        data: params,
        method: "PATCH",
      }),
    useEditConfig(queryKey)
  );

  // const {run, ...asyncResult} = useAsync();
  // const client = useHttp();
  // const mutate = (params: Partial<Project>) => {
  //     return run(
  //         client(`projects/${params.id}`, {
  //             data: params,
  //             method: "PATCH",
  //         })
  //     );
  // };
  // return {mutate, ...asyncResult};
};

/*
 * 添加
 * */
export const useAddProject = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (params: Partial<Project>) =>
      client(`projects`, {
        data: params,
        method: "POST",
      }),
    useAddConfig(queryKey)
  );
  // const {run, ...asyncResult} = useAsync();
  // const client = useHttp();
  // const mutate = (params: Partial<Project>) => {
  //     return run(
  //         client(`projects/${params.id}`, {
  //             data: params,
  //             method: "POST",
  //         })
  //     );
  // };
  // return {mutate, ...asyncResult};
};

/*
 * 删除
 * */
export const useDeleteProject = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    ({ id }: { id: number }) =>
      client(`projects/${id}`, {
        method: "DELETE",
      }),
    useDeleteConfig(queryKey)
  );
  // const {run, ...asyncResult} = useAsync();
  // const client = useHttp();
  // const mutate = (params: Partial<Project>) => {
  //     return run(
  //         client(`projects/${params.id}`, {
  //             data: params,
  //             method: "POST",
  //         })
  //     );
  // };
  // return {mutate, ...asyncResult};
};

export const useProject = (id?: number) => {
  const client = useHttp();
  return useQuery<Project>(
    ["project", { id }],
    () => client(`projects/${id}`),
    { enabled: Boolean(id) }
  );
};
